//
//  UIView+YSFrame.m
//  YSPageViewExample_OC
//
//  Created by MOLBASE on 2017/12/5.
//  Copyright © 2017年 YangShen. All rights reserved.
//

#import "UIView+YSFrame.h"

@implementation UIView (YSFrame)

- (CGFloat)ys_x {
    return self.frame.origin.x;
}

- (void)setYs_x:(CGFloat)x {
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}

- (CGFloat)ys_y {
    return self.frame.origin.y;
}

- (void)setYs_y:(CGFloat)y {
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}

- (CGFloat)ys_width {
    return self.frame.size.width;
}

- (void)setYs_width:(CGFloat)width {
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}

- (CGFloat)ys_height {
    return self.frame.size.height;
}

- (void)setYs_height:(CGFloat)height {
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}

- (CGFloat)ys_centerX {
    return self.center.x;
}

- (void)setYs_centerX:(CGFloat)centerX {
    CGPoint center = self.center;
    center.x = centerX;
    self.center = center;
}

- (CGFloat)ys_centerY {
    return self.center.y;
}

- (void)setYs_centerY:(CGFloat)centerY {
    CGPoint center = self.center;
    center.y = centerY;
    self.center = center;
}

@end

@implementation UIColor (YSRGB)

+ (UIColor *)randomColor {
    return [UIColor colorWithRed:(arc4random() % 256) / 255.0 green:(arc4random() % 256) / 255.0 blue:(arc4random() % 256) / 255.0 alpha:1.0];
}

- (NSArray<NSNumber *> *)getRGBComponents {
    CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
    unsigned char resultingPixel[4];
    CGContextRef context = CGBitmapContextCreate(&resultingPixel,
                                                 1,
                                                 1,
                                                 8,
                                                 4,
                                                 rgbColorSpace,
                                                 1);
    CGContextSetFillColorWithColor(context, [self CGColor]);
    CGContextFillRect(context, CGRectMake(0, 0, 1, 1));
    CGContextRelease(context);
    CGColorSpaceRelease(rgbColorSpace);
    
    return @[@(resultingPixel[0] / 255.0f), @(resultingPixel[1] / 255.0f), @(resultingPixel[2] / 255.0f)];
}

@end
